﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Input;

namespace WpfFunctionTest
{
    public  class CmdFunction
    {
        /// <summary>
        /// cmd进程
        /// </summary>
        public Process CmdProcess;

        public delegate void CmdOutputHandler(string output);
        /// <summary>
        /// 输出事件
        /// </summary>
        public event CmdOutputHandler CmdOutputEvent;
        /// <summary>
        /// 报错事件
        /// </summary>
        public event CmdOutputHandler CmdErrorEvent;

        /// <summary>
        /// 启动cmd进程
        /// </summary>
        public void StartCmdProcess()
        {
            CmdProcess = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };
            CmdProcess.StartInfo = startInfo;
            CmdProcess.OutputDataReceived += (sender, e) =>
            {
                if (!string.IsNullOrEmpty(e.Data))
                {
                    if (CmdOutputEvent!=null)
                    {
                        CmdOutputEvent(e.Data);
                    }
                }
            };

            CmdProcess.ErrorDataReceived += (sender, e) =>
            {
                if (!string.IsNullOrEmpty(e.Data))
                {
                    if (CmdErrorEvent != null)
                    {
                        CmdErrorEvent(e.Data);
                    }
                }
            };
            CmdProcess.Start();
            CmdProcess.BeginOutputReadLine();
            CmdProcess.BeginErrorReadLine();
        }


        /// <summary>
        /// cmd执行命令
        /// </summary>
        public void ExecuteCommand(string cmd)
        {
            if (!string.IsNullOrEmpty(cmd))
            {

                if (cmd == "exit")
                {
                    int ID = CmdProcess.Id;
                    //进程ID是否存在

                    CmdProcess.StandardInput.WriteLine(cmd);
                    Process[] process = Process.GetProcesses();
                    //检查里面有没有ID这个进程
                    foreach (Process p in process)
                    {
                        if (p.Id == ID)
                        {
                            p.Kill();
                            break;
                        }
                    }
                }
                else
                {
                    CmdProcess.StandardInput.WriteLine(cmd);
                }
            }
        }
    
        //退出cmd
        public void ExitCmd()
        {
            if (CmdProcess != null && !CmdProcess.HasExited)
            {
                CmdProcess.Kill();
            }
        }
    }


}
